package com.yinhe.music.yhmusic.play;

import android.app.Service;

import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.db.bean.PlayListDBEntity;
import com.yinhe.music.yhmusic.log.MLog;
import com.yinhe.music.yhmusic.model.Music;

import java.util.ArrayList;
import java.util.List;

public class PlayListManager implements IPlayListManager {
    private Service mService;

    private List<Music> mPlayingList = new ArrayList<>();

    @Override
    public List<Music> getPlayList() {
        return mPlayingList;
    }

    public PlayListManager(Service service) {
        mService = service;
        mPlayingList.addAll(queryPlayList());
    }

    private List<Music> queryPlayList() {
        List<PlayListDBEntity> list = DataBaseAccessor.getInstance().getAllPlayListSyn();
        List<Music> mMusicList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (PlayListDBEntity playListDBEntity : list) {
                Music music = new Music(playListDBEntity);
                music.setRadio(playListDBEntity.isRadio);
                mMusicList.add(new Music(playListDBEntity));
            }
        }
        return mMusicList;
    }

    @Override
    public void addMusicToPlayList(Music music, int position) {
        PlayListDBEntity playListDBEntity = new PlayListDBEntity(music);
        if (!mPlayingList.contains(music)) {
            if (mPlayingList.isEmpty()) {
                mPlayingList.add(music);
            } else {
                if (position < mPlayingList.size()) {
                    mPlayingList.add(position + 1, music);
                } else {
                    mPlayingList.add(music);
                }
            }
        }
        DataBaseAccessor.getInstance().insertPlayList(playListDBEntity);
    }

    @Override
    public void addMusicListToPlayList(List<Music> musicList, int position) {
        List<PlayListDBEntity> playList = new ArrayList<>();
        if (musicList != null && !musicList.isEmpty()) {
            for (Music music : musicList) {
                if (mPlayingList.contains(music)) {
                    removeMusicFromPlayList(music);
                }
                playList.add(new PlayListDBEntity(music));
            }

            if (mPlayingList.isEmpty()) {
                mPlayingList.addAll(musicList);
            } else {
                if (position < mPlayingList.size()) {
                    mPlayingList.addAll(position + 1, musicList);
                } else {
                    mPlayingList.addAll(musicList);
                }
            }

            DataBaseAccessor.getInstance().insertSongListSyn(playList);
        }
    }

    @Override
    public void removeMusicFromPlayList(Music music) {
        mPlayingList.remove(music);
        DataBaseAccessor.getInstance().deleteSongBySongId(music.getSongId());
    }

    @Override
    public void clearPlayList() {
        mPlayingList.clear();
        DataBaseAccessor.getInstance().clearPlayList();
    }

    @Override
    public boolean replacePlayList(List<Music> musicList) {
        boolean isEqual = equalPlayList(musicList);
        MLog.debug("hxh", "isEqual:" + isEqual);
        if (!isEqual) {
            clearPlayList();
            mPlayingList.addAll(musicList);
            List<PlayListDBEntity> playListDBEntityList = setPlayListDb(musicList);
            DataBaseAccessor.getInstance().insertSongListSyn(playListDBEntityList);
        }
        return !isEqual;
    }

    private boolean equalPlayList(List<Music> musicList) {
        if (mPlayingList.size() != musicList.size()) {
            return false;
        } else {
            for (int i = 0; i < mPlayingList.size(); i++) {
                if (mPlayingList.get(i) != musicList.get(i)) {
                    return false;
                }
            }
            return true;
        }
    }

    private List<PlayListDBEntity> setPlayListDb(List<Music> musicList) {
        List<PlayListDBEntity> list = new ArrayList<>();
        for (Music music : musicList) {
            PlayListDBEntity playListDBEntity = new PlayListDBEntity(music);
            list.add(playListDBEntity);
        }
        return list;
    }

}
